const { request } = require('../axios/axios-main')
const env = 'my-book-assist-7gc4zqa8552557ed'

// 按条件获取所有角色
exports.getUsers = (_data) => {
  let data = {}
  const limit = _data.pageSize ? parseInt(_data.pageSize) : 10
  const skip = _data.curPage ? parseInt((_data.curPage - 1)) * limit : 0

  data = {
    env,
    query: `db.collection("users").limit(${limit}).skip(${skip}).get()`
  }

  return request({
    method: 'POST',
    url: `/tcb/databasequery?access_token=${_data.access_token}`,
    data
  })
}

// 获取用户列表总长度
exports.getUsersLength = (_data) => {
  const data = {
    env,
    limit: 10,
    offset: 0
  }
  return request({
    method: 'POST',
    url: `/tcb/databasecollectionget?access_token=${_data.access_token}`,
    data
  })
}

// 查找用户
exports.queryUser = (_data) => {
  _data._id = parseInt(_data._id)
  // 当 _data.pageSize 为空则查找所有用户数据
  // 否则按照分页规则进行查询
  const limit = _data.pageSize ? parseInt(_data.pageSize) : 1000
  let skip = 0
  if (_data.pageSize) {
    skip = _data.curPage ? (parseInt(_data.curPage) - 1) * limit : 0
  }
  let where = ''
  let isUsername = false
  // 根据 _data.username 是否存在进行数据库查询语句拼接
  if (_data.username) {
    isUsername = true
    where = `db.command.and([
      {
        username: db.RegExp({
          regexp: '.*${_data.username}',
          options: 'i'
        })
      }`
    // 根据 _data._id 是否存在进行数据库查询语句拼接
    if (_data._id) {
      where += `,{_id: ${_data._id}}])`
    } else {
      where += `])`
    }
  } else if (_data._id) {
    where = `{_id:${_data._id}}`
  }
  const data = {
    env,
    query: `db.collection('users').where(${where}).limit(${limit}).skip(${skip}).get()`
  }

  return request({
    method: 'POST',
    url: `/tcb/databasequery?access_token=${_data.access_token}`,
    data
  })
}

// 封禁用户
exports.banUser = (_data) => {
  const where = `{
    _id: ${_data._id}
  }`
  const permission = `{
    permission: ${_data.permission}
  }`
  const data = {
    env,
    query: `db.collection('users').where(${where}).update({data:${permission}})`
  }
  return request({
    method: 'POST',
    url: `/tcb/databaseupdate?access_token=${_data.access_token}`,
    data
  })
}

// 新建用户
exports.addUser = (_data) => {
  const access_token = _data.access_token
  delete _data.access_token
  let userJson = JSON.stringify(_data)
  const data = {
    env,
    query: `db.collection('users').add({
      data: [
        ${userJson}
      ]
    })`
  }
  return request({
    method: 'POST',
    url: `/tcb/databaseadd?access_token=${access_token}`,
    data
  })
}

// 获取编辑用户的信息
exports.getEditUser = (_data) => {
  const data = {
    env,
    query: `db.collection('users').where({_id: ${_data._id}}).get()`
  }
  return request({
    method: 'POST',
    url: `/tcb/databasequery?access_token=${_data.access_token}`,
    data
  })
}

// 提交编辑用户请求（云函数）
exports.editUser = (_data) => {
  const data = JSON.parse(JSON.stringify(_data))
  delete data.access_token
  return request({
    method: 'POST',
    url: `/tcb/invokecloudfunction?access_token=${_data.access_token}&env=${env}&name=editUser`,
    data
  })
}
